/*******************************************************************************
* Copyright (c) 2012 
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* zhangliang - initial API and implementation
*******************************************************************************/

#ifndef MODULES_H
#define MODULES_H

#include "config.h"
#include "singleton.h"

typedef void* (*ModuleCreate)(void);
typedef int32_t (*ModuleInit)(void* service, const char* param);
typedef void (*ModuleRelease)(void* context);

class Module {
 public:
  Module(void);
  ~Module(void);

  void*    Create(void);
  int32_t  Init(void* service, const char* param);
  void     Release(void* context);

 private:
  friend class Modules; 

  // module name, name of the *.so file
  std::string   name_;    
  // module ptr
  void*         mod_;
  // three function provided by module
  ModuleCreate  create_;
  ModuleInit    init_;
  ModuleRelease release_;
};

typedef boost::shared_ptr<Module> ModulePtr;

/**
 * @brief
 *  module collection, query/create modules
 */
class Modules : public Singleton<Modules> {
 public:
  /*!
   * @brief
   *  query that if a module with specific name exists
   *  create if non-exists
   */ 
  ModulePtr Query(const std::string& name);

 private:
  boost::mutex mutex_;
  std::list<boost::weak_ptr<Module> > modules_;
};

#endif // MODULES_H

